package edu.wtbu.cs.androiddemo.activity.list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import edu.wtbu.cs.androiddemo.R;

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
    private int[] icons = {R.drawable.a, R.drawable.b, R
            .drawable.c, R.drawable.d, R.drawable.e,
            R.drawable.f, R.drawable.g, R.drawable.h, R
            .drawable.i, R
            .drawable.j, R.drawable.k, R.drawable.l};
    private String[] titles = {"白鹃梅", "五色椒", "小檗",
            "李叶绣菊", "多花兰", "獐耳细辛", "铁茉莉", "三色狸藻", "东洋菊",
            "大花皇冠", "苹果花", "三弄芙蓉"};

    Context context;

    public MyAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.recycler_item,parent,false));

 //       MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.video_recycler_item,parent,false));

        //单击列表项目，触发点击事件
//        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                int pos = myViewHolder.getAdapterPosition();
//                Toast.makeText(context,"你单击的植物是："+titles[pos],Toast.LENGTH_LONG).show();
//            }
//        });

        //单击列表项图片，触发点击事件
        myViewHolder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = myViewHolder.getAdapterPosition();
                Toast.makeText(context,"你单击的植物是："+titles[pos],Toast.LENGTH_LONG).show();
            }
        });
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        holder.image.setImageResource(icons[position]);
        holder.titleView.setText(titles[position]);
    }

    @Override
    public int getItemCount() {
        return titles.length;
    }
}
